package com.orhon.smartcampus.modules.teacher.dao;

import com.orhon.smartcampus.basicComponents.baseDao.BaseDaoImpl;
import com.orhon.smartcampus.modules.base.entity.Grades;
import com.orhon.smartcampus.modules.teacher.entity.TeacherAndGrades;
import com.orhon.smartcampus.modules.teacher.entity.TeacherAndPeriods;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Component;

import javax.transaction.Transactional;
import java.util.List;

@Component
public class TeacherAndGradesDao extends BaseDaoImpl<TeacherAndGrades> {
    @Transactional
    public void insert(TeacherAndGrades entity){
        Session session =this.getSession();
        session.save(entity);
    }
    @Transactional
    public void update(TeacherAndGrades entity){
        Long teacher_id=entity.getTeacher_id();
        Session session =null;
        try {
            session=this.getSession();
            StringBuffer sb = new StringBuffer("delete base_teacher_grades_relation where teacher_id= :teacher_id");
            Query query = session.createSQLQuery(sb.toString()).setParameter("teacher_id",teacher_id);
            //session.update(entity);
            query.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }






    }

    @Transactional
    public List GradesList(Long teacher_id){
        Session session = this.getSession();
        StringBuffer sb = new StringBuffer("select id,teacher_id,grades_id from base_teacher_grades_relation where teacher_id= :teacher_id");
        Query query = session.createSQLQuery(sb.toString()).setParameter("teacher_id",teacher_id);
        List<TeacherAndGrades> teacherAndGrades = this.find("from TeacherAndGrades where teacher_id= " + teacher_id);
        return  teacherAndGrades;
    }
    @Transactional
    public Integer count(Long teacher_id){
        Session session = this.getSession();
        StringBuffer sb = new StringBuffer("select count(*) from base_teacher_grades_relation where teacher_id= :teacher_id");
        Query query = session.createSQLQuery(sb.toString()).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).setParameter("teacher_id",teacher_id);
        return  query.list().size();
    }

}
